//=============================================================================
#include "AppStateGame.h"
#include "AppStateManager.h"
#include "Random.h"
//=============================================================================
CAppStateGame CAppStateGame::Instance;

//=============================================================================
CAppStateGame::CAppStateGame() {
	CSurface c;
	colOK = false;
}

//=============================================================================
void CAppStateGame::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	OnKeyDownInterface(sym);
}

//------------------------------------------------------------------------------
void CAppStateGame::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
	OnKeyUpInterface(sym);
}

void CAppStateGame::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle)
{
	CEntity::mouseX = mX;
	CEntity::mouseY = mY;
}

//=============================================================================
void CAppStateGame::OnActivate() {

	if(CArea::AreaControl.OnLoad("./maps/1.area") == false) {
    	return;
    }

	CClient::getInstance().DoSomething(true);

	//choose spell Rectangle
	cIconRect.x = (WWIDTH/3) + (WWIDTH/16) - 2;
	cIconRect.y = (WHEIGHT/2) + (WHEIGHT/3) + (WHEIGHT/16) + 3;
	cIconRect.h = 36;
	cIconRect.w = 36;

	//X/Y calculations for spell icons
	xCalcIcons = (WWIDTH/3) + (WWIDTH/16);
	yCalcIcons = (WHEIGHT/2) + (WHEIGHT/3) + (WHEIGHT/16) + 5;

	//add images on spell icons
	shieldIcon		 = CSurface::OnLoad("./images/InGameGUI/ShieldSpellIcon.png");
	disruptIcon		 = CSurface::OnLoad("./images/InGameGUI/counterSpellIcon.png");
	fireBallIcon	 = CSurface::OnLoad("./images/InGameGUI/FireballIcon.png");
	magicMissileIcon = CSurface::OnLoad("./images/InGameGUI/MagicMissileIcon.png");
	frostIcon		 = CSurface::OnLoad("./images/InGameGUI/FrostIcon.png");

	//Add image on tabScreen
	tabScreen		= CSurface::OnLoad("./images/InGameGUI/score.png");
	itemScreen      = CSurface::OnLoad("./images/InGameGUI/items.png");
	//Add image on helpscreen
	helpScreen	    = CSurface::OnLoad("./images/InGameGUI/HelpScreen.png");

	backGroundTile = CSurface::OnLoad("./tilesets/background2.png");
	backGroundTile2 = CSurface::OnLoad("./tilesets/background.png");

	//Sound Effects
	CSoundBank::SoundControl.OnLoad("./Sounds/effect02.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect03.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect04.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect05.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect06.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect07.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect08.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect09.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect10.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect11.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect12.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect13.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect14.ogg", 1);
	CSoundBank::SoundControl.OnLoad("./Sounds/effect15.ogg", 1);
	//Music
	CSoundBank::SoundControl.OnLoad("./Sounds/music02.ogg", 0);
	CSoundBank::SoundControl.OnLoad("./Sounds/music03.ogg", 0);
	CSoundBank::SoundControl.OnLoad("./Sounds/music04.ogg", 0);

	//character and effect animation sprites
	Surface_PlayerHurt = CSurface::OnLoad("./images/Characters/FFHit.png");
	Surface_Player = CSurface::OnLoad("./images/Characters/FF.png");
	Surface_FreezeHit = CSurface::OnLoad("./images/Spells/FreezeHit.png");
	Surface_PlayerFrozen = CSurface::OnLoad("./images/Characters/frozen.png");
	Surface_FireBallHit = CSurface::OnLoad("./images/Spells/FireballHit.png");
	Surface_FreezeSpell = CSurface::OnLoad("./images/spells/freeze.png");
	Surface_FireBallSpell = CSurface::OnLoad("./images/spells/fire.png");
	Surface_MagicMissileSpell = CSurface::OnLoad("./images/spells/magicMissile.png");
	Surface_ProjectileSpell = CSurface::OnLoad("./images/spells/projectile.png");

	//Font initialisation
	if( TTF_Init() == -1 )
    {
        return;    
    }
	font = NULL;
	font = TTF_OpenFont("FreeSans.ttf", 18);
	if (font == NULL)
	{
		return;
	}

	IconNameDisplay = "FireBall Spell";

	CCamera::CameraControl.TargetMode = TARGET_MODE_CENTER;

    CCamera::CameraControl.SetTarget(&CEntity::PlayerList.at(CClient::myID)->X, &CEntity::PlayerList.at(CClient::myID)->Y);
}

//-----------------------------------------------------------------------------
void CAppStateGame::OnDeactivate() {
	CArea::AreaControl.OnCleanup();

    //--------------------------------------------------------------------------
    // Entities
    //--------------------------------------------------------------------------
		
    CEntity::PlayerList[0]->OnCleanup();

    CEntity::PlayerList.clear();
	
	
	for(int i = 0;i < CEntity::ProjectileList.size();i++) {
	if(!CEntity::ProjectileList[i]) continue;

	CEntity::ProjectileList[i]->OnCleanup();
	}
	CEntity::ProjectileList.clear();

	SDL_FreeSurface(disruptIcon);
	SDL_FreeSurface(fireBallIcon);
	SDL_FreeSurface(frostIcon);
	SDL_FreeSurface(magicMissileIcon);
	SDL_FreeSurface(shieldIcon);
}


//-----------------------------------------------------------------------------
void CAppStateGame::OnLoop() {

	if (!CSoundBank::SoundControl.IsPlaying())
	{
		CSoundBank::SoundControl.Play(Random(1,3),0);
	}

	// IMPORTANT
	// All changes done here below the RunClient, must also be done in
	// CAppStateServer, below RunServer.

    //--------------------------------------------------------------------------
    // Entities
    //--------------------------------------------------------------------------
    for(int i = 0;i < CEntity::PlayerList.size();i++) {
        if(!CEntity::PlayerList[i]) continue;

        CEntity::PlayerList[i]->OnLoop();
    }

	for(int i = 0;i < CEntity::ProjectileList.size();i++) {
		if(!CEntity::ProjectileList[i]) continue;

		CEntity::ProjectileList[i]->OnLoop();
	}

    //Collision Events
    for(int i = 0;i < CEntityCol::EntityColList.size();i++) {
        CEntity* EntityA = CEntityCol::EntityColList[i].EntityA;
        CEntity* EntityB = CEntityCol::EntityColList[i].EntityB;

        if(EntityA == NULL || EntityB == NULL) continue;

		if(EntityA->playerID == EntityB->playerID) continue;

        if(EntityA->OnCollision(EntityB)) {       
			if(EntityA->Type == 4)
			{
				EntityB->OnCollision(EntityA);		
			}		
			else
			{
				EntityB->OnCollision(EntityA);
			}
		}
   }
	for(int i = 0;i < NetworkCol::NetworkEntityColList.size();i++) {
		if(NetworkCol::NetworkEntityColList[i].A < CEntity::PlayerList.size() && NetworkCol::NetworkEntityColList[i].B < CEntity::ProjectileList.size())
		{
			CEntity* EntityA = CEntity::PlayerList.at(NetworkCol::NetworkEntityColList[i].A);
			CEntity* EntityB = CEntity::ProjectileList.at(NetworkCol::NetworkEntityColList[i].B);

			if(EntityA == NULL || EntityB == NULL) continue;    
		
			EntityB->RunCollision(EntityA);
			EntityA->RunCollision(EntityB);
			
			colOK = true;
		}
   }

	if(colOK)
	{
		NetworkCol::NetworkEntityColList.clear();
		colOK = false;
	}
    CEntityCol::EntityColList.clear();
	
}

//-----------------------------------------------------------------------------
void CAppStateGame::OnRender(SDL_Surface* Surf_Display) {
		

		SDL_Rect Rect;


		/*--------------------------------------------*/                                           
		/* Checks where player is located and sets     !
		/* the camera target so the player dont see    !
		/*  outside the level.                         ! 
		/*--------------------------------------------*/
	
		if(CEntity::PlayerList.at(CClient::myID)->X<=WWIDTH/2)
		{
			CCamera::CameraControl.SetTarget(0,&CEntity::PlayerList.at(CClient::myID)->Y); 
		}
	
		if(CEntity::PlayerList.at(CClient::myID)->X>=WWIDTH/2)
		{	
			CCamera::CameraControl.SetTarget(&CEntity::PlayerList.at(CClient::myID)->X,&CEntity::PlayerList.at(CClient::myID)->Y);
		}
	
		if(CEntity::PlayerList.at(CClient::myID)->X<=((WWIDTH/2)*5) && CEntity::PlayerList.at(CClient::myID)->X>WWIDTH/2)
		{
			CCamera::CameraControl.SetTarget(&CEntity::PlayerList.at(CClient::myID)->X,&CEntity::PlayerList.at(CClient::myID)->Y); 
		}
	
		if(CEntity::PlayerList.at(CClient::myID)->X>=(((TILE_SIZE*MAP_WIDTH)*3)-(WWIDTH/2)))
		{
			float xMax = ((TILE_SIZE*MAP_WIDTH)*3)-(WWIDTH/2);
			CCamera::CameraControl.SetTarget(&xMax,&CEntity::PlayerList.at(CClient::myID)->Y);
		}
	
		SDL_FillRect(Surf_Display, &Rect, 0);
		

		float a = CCamera::CameraControl.GetX() / 4096;
		float b = CCamera::CameraControl.GetY() / 4096;
		float c = 1.2; //speed of the movement compare to camera-speed
		float d = 1.1; //speed of the movement compare to camera-speed

		//Drawing background and 
		for(int i = 0; i < 16; i++)
		{
			for(int j = 0; j < 16; j++)
			{
				CSurface::OnDraw(Surf_Display, backGroundTile, (256*i)+((256*a)-CCamera::CameraControl.GetX())/c, (256*j)+((256*b)-CCamera::CameraControl.GetY())/c);
				//CSurface::OnDraw(Surf_Display, backGroundTile2, (512*i)+((512*a)-CCamera::CameraControl.GetX())/1.1, (512*j)+((512*b)-CCamera::CameraControl.GetY())/1.1);
			}
		}

		
		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j < 8; j++)
			{
				//CSurface::OnDraw(Surf_Display, backGroundTile, (256*i)+((256*a)-CCamera::CameraControl.GetX())/1.2, (256*j)+((256*b)-CCamera::CameraControl.GetY())/1.2);
				CSurface::OnDraw(Surf_Display, backGroundTile2, (512*i)+((512*a)-CCamera::CameraControl.GetX())/d, (512*j)+((512*b)-CCamera::CameraControl.GetY())/1);
			}
		}


		CArea::AreaControl.OnRender(Surf_Display, -CCamera::CameraControl.GetX(), -CCamera::CameraControl.GetY());
	
	    //--------------------------------------------------------------------------
	    // Entities
	    //--------------------------------------------------------------------------
	    for(int i = 0;i < CEntity::PlayerList.size();i++) {
			if(!CEntity::PlayerList[i]) continue;
	
			if(CEntity::PlayerList[i] != NULL)
				CEntity::PlayerList[i]->OnRender(Surf_Display,i);
	    }
	
		//for(int i = 0;i < CEntity::PlayerList.size();i++)
		//{
		//	CEntity* EntityB = CEntity::PlayerList[i];
		//	if(EntityB->exploded == true)
		//		{
		//			int explosionPosX = EntityB->explosionX;
		//			int explosionPosY = EntityB->explosionY; 
		//			int x = CCamera::CameraControl.GetX();
		//			int y = CCamera::CameraControl.GetY();
		//			CExplosion * c = new CExplosion;
		//			c->Run(10,explosionPosX-x,explosionPosY-y,Surf_Display,c);	
		//			EntityB->explosionX = 0;
		//			EntityB->explosionY = 0;
		//			EntityB->exploded = false;
		//		}
		// }

		for(int i = 0;i < CEntity::ProjectileList.size();i++) {
			if(!CEntity::ProjectileList[i]) continue;

			if(CEntity::ProjectileList[i] != NULL)
				CEntity::ProjectileList[i]->OnRender(Surf_Display,1000);
		}
	
		OnRenderInterface(Surf_Display);

		//if(CExplosion::Render == false)
		//{
		//	CExplosion::Render = true;
		//	SDL_Flip(Surf_Display);
		//	CExplosion::Render = false;
		//}
		
}

//=============================================================================
CAppStateGame* CAppStateGame::GetInstance() {
	return &Instance;
}

//=============================================================================

